package com.bto.solar.work.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bto.solar.work.constant.DBConstants;
import com.bto.solar.work.entity.User;
import com.bto.solar.work.mapper.UserMapper;
import com.bto.solar.work.dao.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bto.solar.work.utils.NullUtils;
import com.bto.solar.work.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author adrian
 * @since 2023-08-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Page<User> getUserByPage(Long loginUserId, Long companyId, Byte status, String queryValue,
                                    List<Long> userIds, Long queryUserId,
                                    Page<User> userPage, String approve, boolean orderBy, String permissionName) {

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        //不等于自己登陆ID
        queryWrapper.ne(User::getUserId, loginUserId);

        //如果不是管理员和不是总部用户，就要部门限制
        //如果不是超级管理员，就要查询自身的可创建业务权限的用户ID
        //查询条件-可创建业务类型
        if (NullUtils.isNotEmpty(userIds)) {
            queryWrapper.in(User::getUserId, userIds);
        } else {
            return new Page<>();
        }

        //查询条件-公司类型
        if (NullUtils.isNotNull(companyId)) {
            queryWrapper.eq(User::getCompanyId, companyId);
        }

        //查询条件-状态类型
        if (NullUtils.isNotNull(status)) {
            queryWrapper.eq(User::getStatus, status);
        }

        //查询条件--权限
        if (StringUtils.isNotBlank(permissionName)) {
            queryWrapper.like(User::getPermissionIds, permissionName);
        }

        //查询条件-姓名或手机后或邮箱或者账号
        if (StringUtils.isNotEmpty(queryValue)) {
            queryWrapper.and(
                    wrapper -> wrapper.like(User::getName, queryValue)
                            .or().like(User::getPhone, queryValue)
                            .or().like(User::getEmail, queryValue)
                            .or().like(User::getAccount, queryValue));
        }

        //查询条件-用户ID (不是业务需求)
        if (NullUtils.isNotNull(queryUserId)) {
            queryWrapper.eq(User::getUserId, queryUserId);
        }

        //审核
        if (DBConstants.USER_APPROVE_APPROVED.equals(approve)) {
            queryWrapper.ne(User::getApprove, DBConstants.USER_APPROVE_WAITING)
                    .eq(User::getSource, DBConstants.USER_SOURCE_APP);
        } else {
            queryWrapper.eq(User::getApprove, approve);
        }

        if (orderBy) {
            queryWrapper.eq(User::getDeleted, false).orderByDesc(User::getUpdateTime);
        } else {
            queryWrapper.eq(User::getDeleted, false).orderByAsc(User::getUpdateTime);
        }

        return userMapper.selectPage(userPage, queryWrapper);
    }
}
